package weaver.splittable.biz.workflow;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 流程相关分表匹配SQL工具
 * author: YUEYANG 2023-01-12
 */
public class STWorkflowMatchUtil {

    // 匹配SQL where中带有 requestid = > < >= <= 条件的
    private static final String WHERE_REQUEST_ID_REGEX = "requestid *(=|>|<|>=|<=) *'?\"?\\d+'?\"?";

    // 匹配SQL where中带有 createdate = > < >= <= 条件的
    private static final String WHERE_CREATE_DATE_REGEX = "createdate *(=|>|<|>=|<=) *'?\"?[\\d|\\-]+'?\"?";

    // 匹配SQL where中带有 requestid in 条件的
    private static final String WHERE_REQUEST_ID_IN_REGEX = "requestid *in *\\(( *'?\"?\\d+'?\"? *,? *)+\\)";

    // 匹配SQL where中带有 createdate in 条件的
    private static final String WHERE_CREATE_DATE_IN_REGEX = "createdate *in *\\(( *'?\"?[\\d,-]+'?\"? *,?)+\\)";

    /**
     * 获取SQL中需要的where条件
     * requestid相关条件 >, <, =, >=, <=, in
     * createdate相关条件 >, <, =, >=, <=, in
     * @param sql 待执行SQL
     * @return 所有where条件
     */
    public static Map<String, List<STWorkflowWhereInfo>> getWheres(String sql) {
        Map<String, List<STWorkflowWhereInfo>> whereMap = new HashMap<>();
        List<STWorkflowWhereInfo> requestIdInfos = new ArrayList<>();
        List<STWorkflowWhereInfo> createDateInfos = new ArrayList<>();
        whereMap.put("requestid", requestIdInfos);
        whereMap.put("createdate", createDateInfos);

        sql = sql.toLowerCase();

        // 匹配提取requestid的where非in条件
        Pattern p = Pattern.compile(WHERE_REQUEST_ID_REGEX);
        Matcher m = p.matcher(sql);
        while(m.find()) {
            String whereStr = m.group();
            String operator = getOperator(whereStr);
            String right = StringUtils.deleteWhitespace(whereStr).toLowerCase().replace("requestid" + operator, "");
            if (right.contains("'")) {
                right = right.replace("'", "");
            }
            if (right.contains("\"")) {
                right = right.replace("\"", "");
            }
            Integer rightInteger = Integer.valueOf(right);
            STWorkflowWhereInfo whereInfo = new STWorkflowWhereInfo("requestid", operator, rightInteger);
            requestIdInfos.add(whereInfo);
        }
        // 匹配提取requestid的where in条件
        p = Pattern.compile(WHERE_REQUEST_ID_IN_REGEX);
        m = p.matcher(sql);
        while (m.find()) {
            String whereStr = m.group();
            String inListStr = StringUtils.deleteWhitespace(whereStr.substring(whereStr.indexOf("(")+1, whereStr.indexOf(")")));
            String[] inListArray = inListStr.split(",");
            List<Object> inItems = new ArrayList<>();
            for (String inItem: inListArray) {
                if (inItem.contains("'")) {
                    inItem = inItem.replace("'", "");
                }
                if (inItem.contains("\"")) {
                    inItem = inItem.replace("\"", "");
                }
                inItems.add(Integer.valueOf(inItem));
            }
            STWorkflowWhereInfo whereInfo = new STWorkflowWhereInfo("requestid", inItems);
            requestIdInfos.add(whereInfo);
        }

        // 匹配提取createdate的where非in条件
        p = Pattern.compile(WHERE_CREATE_DATE_REGEX);
        m = p.matcher(sql);
        while(m.find()) {
            String whereStr = m.group();
            String operator = getOperator(whereStr);
            String right = StringUtils.deleteWhitespace(whereStr).toLowerCase().replace("createdate" + operator, "");
            STWorkflowWhereInfo whereInfo = new STWorkflowWhereInfo("createdate", operator, right);
            createDateInfos.add(whereInfo);
        }
        // 匹配提取createdate的where in条件
        p = Pattern.compile(WHERE_CREATE_DATE_IN_REGEX);
        m = p.matcher(sql);
        while (m.find()) {
            String whereStr = m.group();
            String inListStr = StringUtils.deleteWhitespace(whereStr.substring(whereStr.indexOf("(")+1, whereStr.indexOf(")")));
            String[] inListArray = inListStr.split(",");
            List<Object> inItems = new ArrayList<>();
            for (String inItem: inListArray) {
                if (inItem.contains("'")) {
                    inItem = inItem.replace("'", "");
                }
                if (inItem.contains("\"")) {
                    inItem = inItem.replace("\"", "");
                }
                inItems.add(inItem);
            }
            STWorkflowWhereInfo whereInfo = new STWorkflowWhereInfo("createdate", inItems);
            createDateInfos.add(whereInfo);
        }
        return whereMap;
    }



    private static String getOperator(String whereStr) {
        if (whereStr.contains(">=")) {
            return ">=";
        }
        if (whereStr.contains("<=")) {
            return "<=";
        }
        if (whereStr.contains("=")) {
            return "=";
        }
        if (whereStr.contains(">")) {
            return ">";
        }
        if (whereStr.contains("<")) {
            return "<";
        }
        return "";
    }

}
